﻿using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;

namespace ZhuXingFamilys.ExportExcel.utils
{
    public class ElementsData
    {
        public static List<excelData> getExcelDataList(Document doc, BackgroundWorker backWork, string currentDocument = "current")
        {
            int id = 1;
            double index = 1;
            int progress = 0;
            List<excelData> excelDataList = new List<excelData>();
            // var collector = new FilteredElementCollector(doc);
            // collector.OfClass(classType);
            // FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id);
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            collector.WhereElementIsNotElementType();
            int count = collector.ToElements().Count;
            foreach (Element element in collector.ToElements())
            {
                // 当前进度
                var d = index / count * 100;
                if (progress != (int)d)
                {
                    progress = (int)d;
                    backWork.ReportProgress(progress, currentDocument);
                }
                index++;

                Element ele = doc.GetElement(element.GetTypeId());
                if (ele == null)
                    continue;
                // 判断是否有族库编号参数
                Parameter familyNumber = ele.LookupParameter("族库编号");
                if (familyNumber == null || string.IsNullOrEmpty(familyNumber.AsString()))
                    continue;

                // 封装实体
                excelData excel = new excelData();
                // 类型参数部分
                excel.Code = familyNumber.AsString();
                excel.FamilyNumber = familyNumber.AsString();
                excel.SpecialitieId = int.Parse(excel.FamilyNumber.Substring(0, 2));
                Parameter company = ele.LookupParameter("计量单位");
                if (company != null)
                    excel.Company = company.AsString();
                Parameter features = ele.LookupParameter("项目特征");
                if (features != null)
                    excel.Features = features.AsString();

                // 公共参数部分
                // ID
                excel.Id = id;
                // 族ID
                excel.elementId = element.Id.IntegerValue.ToString();
                // 类型
                Parameter type = element.get_Parameter(BuiltInParameter.ELEM_TYPE_PARAM);
                if (type != null)
                    excel.Type = type.AsValueString();
                // 类型ID
                Parameter typeId = element.get_Parameter(BuiltInParameter.SYMBOL_ID_PARAM);
                if (typeId != null)
                    excel.TypeId = typeId.AsValueString();
                // 族与类型
                Parameter name = element.get_Parameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM);
                if (name != null)
                    excel.Name = name.AsValueString();
                // 类别
                excel.Category = element.Category.Name;
                // Level
                Element level = doc.GetElement(element.LevelId);
                if (level != null)
                    excel.Elevation = level.Name;
                // 体积
                Parameter volume = element.get_Parameter(BuiltInParameter.HOST_VOLUME_COMPUTED);
                if (volume != null)
                    excel.Volume = Math.Round(UnitUtils.ConvertFromInternalUnits(volume.AsDouble(), volume.DisplayUnitType), 3);
                // 面积
                Parameter area = element.get_Parameter(BuiltInParameter.HOST_AREA_COMPUTED);
                if (area != null)
                    excel.Area = Math.Round(UnitUtils.ConvertFromInternalUnits(area.AsDouble(), area.DisplayUnitType), 3);
                // 长度
                Parameter length = element.get_Parameter(BuiltInParameter.CONTINUOUS_FOOTING_LENGTH);
                if (length != null)
                    excel.Length = Math.Round(UnitUtils.ConvertFromInternalUnits(length.AsDouble(), length.DisplayUnitType), 3);

                // 特殊参数部分 没有特殊情况无需设置 如果有获取不到的参数可以分情况设置
                // FamilyInstance
                if (element is FamilyInstance)
                {
                    // 机械设备
                    if ((BuiltInCategory)element.Category.Id.IntegerValue == BuiltInCategory.OST_MechanicalEquipment)
                    {
                        // 井
                        if (excel.Type.Contains("井"))
                        {
                            length = element.LookupParameter("深度");
                            if (length != null)
                                excel.Length = Math.Round(UnitUtils.ConvertFromInternalUnits(length.AsDouble(), length.DisplayUnitType), 3);
                        }
                    }
                    // 结构基础
                    if ((BuiltInCategory)element.Category.Id.IntegerValue == BuiltInCategory.OST_StructuralFoundation)
                    {
                        // 桩
                        if (excel.Type.Contains("桩"))
                        {
                            length = element.LookupParameter("桩长度");
                            if (length != null)
                                excel.Length = Math.Round(UnitUtils.ConvertFromInternalUnits(length.AsDouble(), length.DisplayUnitType), 3);
                        }
                    }

                }
                // Wall
                if (element is Wall)
                {

                }
                // Ceiling
                if (element is Ceiling)
                {

                }
                // Floor
                if (element is Floor)
                {

                }
                // ExtrusionRoof
                if (element is ExtrusionRoof)
                {

                }
                // FootPrintRoof
                if (element is FootPrintRoof)
                {

                }
                // Pipe
                if (element is Pipe)
                {
                    // 管道长度
                    length = element.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH);
                    if (length != null)
                        excel.Length = Math.Round(UnitUtils.ConvertFromInternalUnits(length.AsDouble(), length.DisplayUnitType), 3);
                    // 管道面积
                    area = element.get_Parameter(BuiltInParameter.RBS_CURVE_SURFACE_AREA);
                    if (area != null)
                        excel.Area = Math.Round(UnitUtils.ConvertFromInternalUnits(area.AsDouble(), area.DisplayUnitType), 3);
                    // 参照标高
                    excel.Elevation = element.get_Parameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsValueString();
                }
                // PipingSystem
                if (element is PipingSystem)
                {

                }
                // Railing
                if (element is Railing)
                {
                    // 扶手栏杆长度
                    if ((BuiltInCategory)element.Category.Id.IntegerValue == BuiltInCategory.OST_StairsRailing)
                    {
                        length = element.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH);
                        if (length != null)
                            excel.Length = Math.Round(UnitUtils.ConvertFromInternalUnits(length.AsDouble(), length.DisplayUnitType), 3);
                    }
                }
                // TopRail
                if (element is TopRail)
                {

                }

                // 设置单位
                excel.NumCount = 1; //计量数量
                excel.Quantitiesname = "面积"; //工程量名称
                excel.Num = excel.Area; // 工程数量

                if (excel.Company == "㎡" || excel.Company == "m2")
                {
                    excel.Quantitiesname = "面积";
                    excel.Num = excel.Area;
                }
                else if (excel.Company == "m³" || excel.Company == "m3")
                {
                    excel.Quantitiesname = "体积";
                    excel.Num = excel.Volume;
                }
                else if (excel.Company == "m")
                {
                    excel.Quantitiesname = "长度";
                    excel.Num = excel.Length;
                }
                //else if (excel.Company.Contains("间") || excel.Company.Contains("座") || excel.Company.Contains("个"))
                //{
                //    excel.Quantitiesname = "数量";
                //    excel.Num = excel.NumCount;
                //}
                else
                {
                    excel.Quantitiesname = "数量";
                    excel.Num = excel.NumCount;
                }
                excelDataList.Add(excel);
                id++;
            }
            return excelDataList;
        }
    }
}